package com.nari.ies.ne.nw.server.service.fileQuality.impl;

import com.nari.ies.ne.nw.server.entity.FileQualityEntity;
import com.nari.ies.ne.nw.server.service.fileQuality.IDQYCFileQualityService;
import com.nari.ies.ne.nw.server.util.IoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.*;
@Slf4j
@Service
public class DQYCFileQualityServiceImpl implements IDQYCFileQualityService {

    @Override
    public FileQualityEntity DQYCFileQuality(File file, FileQualityEntity fileQualityEntity) {
        // 文件是否延时
        int fileDelay = 1;
        // 文件内容是否为空
        int empty = 0;
        // 文件标签是否缺失
        int missingTag = 0;
        // 文件内容是否缺失
        int defect = 0;
        //文件格式是否有误（数据列分割后数目不对）
        int fileFormatError = 0;
        //文件内容格式是否有误（数据列类型转换报错）
        int fileContentError = 0;

        // 信息
        String errorInfo = fileQualityEntity.getMessage();
        //标题长度
        int bwdCount = 0;

        // 判断文件内容是否为空
        if (0 == file.length()) {
            errorInfo = "文件内容为空";
            fileQualityEntity.setIsFileDelay(fileDelay);
            fileQualityEntity.setIsFileDefect(defect);
            fileQualityEntity.setIsFileEmpty(empty);
            fileQualityEntity.setMessage(errorInfo);
            fileQualityEntity.setIsFileMissTag(missingTag);
            fileQualityEntity.setIsFileFormatError(fileFormatError);
            fileQualityEntity.setIsFileContentError(fileContentError);
            return fileQualityEntity;
        } else {
            empty = 1;
            // 判断标签
            FileReader fr = null;
            BufferedReader br = null;
            try {
                fr = new FileReader(file);
                br = new BufferedReader(fr);
                String line = "";
                // 记录标签行数
                int labelCount = 0;

                String fileType = fileQualityEntity.getFileType();// 文件类型
                String plantCode = fileQualityEntity.getPlantCode();// 场站code
                String firstLabel = "<" + fileType + "::" + plantCode;
                String endLabel = "</" + fileType + "::" + plantCode + ">";
                while ((line = br.readLine()) != null) {
                    if (line.length() == 0) {
                        continue;
                    }
                    String[] strs = line.split("\t");

                    // 判断标签是否缺失不符合规范
                    if (line.contains("<")) {

                        if (1 == labelCount) {
                            if (!(firstLabel.equals(strs[0]))) {
                                errorInfo = "文件标签缺失";
                                break;
                            }
                        }
                        if (2 == labelCount) {
                            if (!(endLabel.equals(strs[0]))) {
                                errorInfo = "文件标签缺失";
                                break;
                            } else {
                                missingTag = 1;
                            }
                        }
                        labelCount++;
                    }

                    if (line.contains("@")) {
                        bwdCount = strs.length;
                    }

                    // 判断内容是否缺失
                    if (line.contains("#")) {
                        defect = 1;
                        if (bwdCount != strs.length) {
                            errorInfo = "文件格式错误";
                            break;
                        } else {
                            fileFormatError = 1;
                            for (int j = 2; j < strs.length; j++) {
                                if (!"null".equals(strs[j]) && !"NULL".equals(strs[j]) && !" ".equals(strs[j])) {
                                    Float.parseFloat(strs[j]);
                                }
                            }
                            fileContentError = 1;
                        }
                    }
                }

                if (defect == 0) {
                    if(!errorInfo.equals("文件标签缺失".trim())){
                        errorInfo = "文件内容缺失";
                    }
                }

            } catch (IOException e) {
                e.printStackTrace();
            } catch (NumberFormatException e) {
                errorInfo = "文件内容格式错误";
            } finally {
                IoUtil.close(br);
                IoUtil.close(fr);
                fileQualityEntity.setIsFileDelay(fileDelay);
                fileQualityEntity.setIsFileDefect(defect);
                fileQualityEntity.setIsFileEmpty(empty);
                fileQualityEntity.setMessage(errorInfo);
                fileQualityEntity.setIsFileMissTag(missingTag);
                fileQualityEntity.setIsFileFormatError(fileFormatError);
                fileQualityEntity.setIsFileContentError(fileContentError);
                return fileQualityEntity;
            }
        }
    }

}
